#!/user/bin/env python3
# -*- coding: utf-8 -*-
"""
--------------------------------------
    Author:     JiChao_Song
    Date  :     2021/11/28 16:34
    Desc  :
--------------------------------------
"""
import json
import time
from django.db import connection
from django.utils.crypto import get_random_string

from common.enums import AbstractBaseEnum
from common.enums.base_enums import OperateLogModelTypeEnums, TagColorEnum, TagColorRGBEnum
from common.enums.case_enums import CaseStatusEnum, CaseImportanceEnum
from common.utils.log_utils import logger


def get_time():
    return int(time.time() * 1000)


def sorted_dict(array: list, dict_field: str):
    """
    字典数组排序
    :param array: 字典数组
    :param dict_field: 字典一级字段名称
    :return:
    """
    dict_field_list = []
    dict_field_list_sort = []
    for i in array:
        dict_field_list.append(i.get(dict_field))
    dict_field_list = sorted(dict_field_list)
    for i in dict_field_list:
        for j in array:
            if j.get(dict_field) == i:
                dict_field_list_sort.append(j)

    return dict_field_list_sort


def get_random_secret_key():
    """
    Return a 50 character random string usable as a SECRET_KEY setting value.
    """
    chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(=+)'
    return get_random_string(50, chars)


def queryDict(sql, params=None):
    """
    查询结果返回字典类型数据
    :param sql:
    :param params:
    :return:
    """
    logger.info(f"==> Preparing: {sql}")
    logger.info(f"==> Parameters: {params if params else ''}")

    with connection.cursor() as cursor:
        cursor.execute(sql, params=params)
        col_names = [desc[0] for desc in cursor.description]
        row = cursor.fetchall()
        rowList = []
        for list in row:
            tMap = dict(zip(col_names, list))
            rowList.append(tMap)
        logger.info(msg = f'==> total: {len(rowList)}')
        return rowList


def get_enum_str_list(enumObj):
    enum_str = []
    for i in dir(enumObj):

        if isinstance(eval(f'enumObj.{i}'), AbstractBaseEnum):
            enum_str.append(eval(f'enumObj.{i}').getStringKeyValue())
    return sorted(enum_str)


def get_enum_key_list(enumObj):
    enum_str = []
    for i in dir(enumObj):

        if isinstance(eval(f'enumObj.{i}'), AbstractBaseEnum):
            enum_str.append(eval(f'enumObj.{i}').key)
    return sorted(enum_str)


def get_enum_key_value_list(enumObj):
    enum_str = []
    for i in dir(enumObj):

        if isinstance(eval(f'enumObj.{i}'), AbstractBaseEnum):
            enum_str.append(dict(
                dataKey=eval(f'enumObj.{i}').key,
                dataValue=eval(f'enumObj.{i}').value,
                dataDesc=None,
            ))
    return sorted_dict(enum_str, 'dataKey')


def get_enum_key_by_value(enumObj, value):
    data = None
    for i in dir(enumObj):
        if isinstance(eval(f'enumObj.{i}'), AbstractBaseEnum):
            if eval(f'enumObj.{i}').value == value:
                data = eval(f'enumObj.{i}').key
                break
    return data


def get_enum_value_by_key(enumObj, key):
    enum_str = []
    for i in dir(enumObj):

        if isinstance(eval(f'enumObj.{i}'), AbstractBaseEnum):
            enum_str.append(eval(f'enumObj.{i}').key)
    return sorted(enum_str)


if __name__ == '__main__':
    print(get_enum_key_by_value(enumObj=CaseStatusEnum, value='草稿'))
    print(get_enum_str_list(enumObj = CaseStatusEnum))